#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/8/29 下午10:32
# @Author  : Liujiaqi
# @Site    : 
# @File    : RBACObject.py
# @Software: PyCharm

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)
    password = db.Column(db.String(120))
    is_super_user = db.Column(db.Boolean,default=False)
    is_active = db.Column(db.Boolean,default=True)
    create_time = db.Column(db.DateTime)
    roles = db.Column(db.String)
    group_id = db.relationship('UserGroup', backref='User',lazy='dynamic')

    def __init__(self, username, email,password,is_super_user,is_active,create_time,group_id,roles):
        self.username = username
        self.email = email
        self.password = password
        self.is_super_user = is_super_user
        self.is_active = is_active
        self.create_time = create_time
        self.group_id = group_id
        self.roles = roles

    def __repr__(self):
        return '<User %r>' % self.username

class UserGroup(db.Model):
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    groupname = db.Column(db.String(80), unique=True)
    roles = db.Column(db.String)
    is_active = db.Column(db.Boolean,default=True)

    def __init__(self,groupname,roles,is_active):
        self.groupname = groupname
        self.roles = roles
        self.is_active = is_active

    def __repr__(self):
        return '<GroupName %r>' % self.groupname

class Role(db.Model):
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    rolename = db.Column(db.String(80), unique=True)
    roleurl = db.Column(db.String(180), unique=True)
    is_active = db.Column(db.Boolean,default=True)

    def __init__(self,rolename,roleurl,is_active):
        self.rolename = rolename
        self.roleurl = roleurl
        self.is_active = is_active

    def __repr__(self):
        return '<Role %r>' % self.rolename
